<template>
  <el-dialog :title="title" :visible.sync="dialogFormVisible">
    <el-form ref="modalForm" :model="dataSource" status-icon>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item v-if="modalType === 'edit'" label="房屋信息编号" :label-width="formLabelWidth">
            <el-input v-model="dataSource.id" disabled autocomplete="off" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item v-if="modalType === 'edit'" label="住户名称" :label-width="formLabelWidth">
            <el-input v-model="dataSource.householdName" disabled autocomplete="off" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-row :gutter="24">
        <el-col :span="12">
          <el-form-item
            label="房号："
            prop="buildingNumber"
            :label-width="formLabelWidth"
            :rules="[
              { required: true, message: '房号不能为空',trigger:'blur,change'},
              { min: 1,max:3, message: '房号长度在1-3个字符之间',trigger:'change'}
            ]"
          >
            <el-input v-model="dataSource.houseNumber" placeholder="请输入房屋的房号" autocomplete="off" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item
            label="建筑面积："
            prop="houseArea"
            :label-width="formLabelWidth"
            :rules="[
              { required: true, message: '建筑面积不能为空',trigger:'blur,change'}
            ]"
          >
            <el-input v-model="dataSource.houseArea" placeholder="请输入房屋的建筑面积" autocomplete="off">
              <span slot="suffix">
                /㎡
              </span>
            </el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="dialogFormVisible = false">取 消</el-button>
      <el-button type="primary" @click="handleConfirm('modalForm')">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addOneHouseInfo, modifyOneHouseInfo } from '@/api/ht/xqzhgl/fwgl/fwxxgl'

const { Message } = require('element-ui')
export default {
  name: 'Modal',
  props: {
    houseInfo: {
      type: Object,
      default: () => {
        return {
          id: '',
          houseNumber: '',
          buildingCode: '',
          houseArea: ''
        }
      }
    }
  },
  data() {
    return {
      title: '住户房屋信息',
      dialogFormVisible: false, // 是否展示对话框
      formLabelWidth: '150px', // 表单输入框字段宽度
      modalType: 'add',
      dataSource: { // 数据源
      }
    }
  },
  watch: {
    houseInfo(newdata, prevdata) {
      this.dataSource = newdata
      this.title = '住户房屋信息'
    }
  },
  methods: {
    checkModelType(type) {
      this.modalType = type
      // 展示对话框
      this.dialogFormVisible = true
      // 判断是编辑还是新增
      if (type === 'add') {
        // 置空
        this.title = '供热&设备信息-新增'
        this.dataSource = {}
      }
    },
    handleConfirm(formName) {
      const type = this.modalType
      // 判断是编辑还是新增
      if (type === 'add') {
        // 调用新增请求
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.addHouseInfoHandle()
          } else {
            console.log('表单缺少数据或者不符合要求')
            return false
          }
        })
      } else {
        // 调用修改请求
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.modifyHouseInfoHandle()
          } else {
            console.log('表单缺少数据或者不符合要求')
            return false
          }
        })
      }
    },
    addHouseInfoHandle() {
      this.addHouseInfo()
    },
    modifyHouseInfoHandle() {
      this.modifyHouseInfo()
    },
    async addHouseInfo() {
      const res = await addOneHouseInfo(this.dataSource)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'Error',
          type: 'success',
          duration: 2 * 1000
        })
        this.dialogFormVisible = false
        this.reflushDataHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    async modifyHouseInfo() {
      const res = await modifyOneHouseInfo(this.dataSource)
      const resp = res.data.respOutMsgHeader
      if (resp.respCode === 200) {
        Message({
          message: resp.msg || 'Error',
          type: 'success',
          duration: 2 * 1000
        })
        this.dialogFormVisible = false
        this.reflushDataHandle()
      } else {
        Message({
          message: resp.msg || 'Error',
          type: 'error',
          duration: 2 * 1000
        })
      }
    },
    reflushDataHandle() {
      // 调用父组件获取方法刷新数据
      this.$emit('reflushDataHandle')
    }
  }
}
</script>

<style scoped>

</style>
